home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / gedscripts / pastex / texedit.rexx < prev   
OS/2 REXX Batch file  |  1995-03-09  |  13KB  |  392 lines

  1. /*RX
  2.  * AREXX        Name:TeXedit.rexx       version:1.41    date:27-Jul-91
  3.  *
  4.  This ARexx script is called from virtex (or initex) in case of an
  5. error or if the 'e' command is used, and it's given the current file
  6. and line number as argument. It may be called from the TeX-server too.
  7. We will successively call CygnusEd (CED) or Micro(GNU)Emacs (MG) and GoldED to
  8. load the file and the logfile. Other editors welcome!
  9.  *
  10. INPUTS:
  11.  1: filename to edit (please no spaces in filename)
  12.  2: line where error occured
  13.  *
  14. BUGS:
  15.  GETCLIP("TEXFILE") gets precedence over any other information for
  16. retrieving the logfile, so if you alternately start virtex directly
  17. from CLI while the server is active, the wrong files may be loaded.
  18.  *
  19.  This file tries to cope with the following version (PasTeX 1.2a)
  20. > This is a PD-Version of Pas-TeX (made Jan 30 1991 [br]/[hes])
  21. > This is TeX, C Version 3.1
  22. (The logfile is sometimes put at the wrong place.)
  23.  >> I have only tried with Version 3.14 (Rene Laederach)
  24.  *
  25.  Does not handle names relative to the local root correctly (like ":foo/bar")
  26.  *
  27.  See each editor relative bugs below.
  28.  *
  29. FILES:
  30.  Rexx:namestruc
  31.  LIBS:rexxsupport.library
  32. AUTHORS:
  33.  *      J\"org H\"ohle, since March 91
  34.  *      Georg Hessmann, previous version
  35.  *      Ren\'e\ Laederach for GoldED part
  36.  */
  37.  
  38. /* call trace('all') */
  39.  
  40. /**
  41. TeXRexx  = "rexx:TeX"          / * where our scripts stay * /
  42. namestruc= TeXRexx||namestruc
  43. **/
  44.  
  45. IF ~SHOW('L','rexxsupport.library') THEN
  46.         IF ~ADDLIB('rexxsupport.library',0,-30,0) THEN DO
  47.                 say "Can't open 'rexxsupport.library'!"
  48.                 EXIT 20
  49.                 END
  50.  
  51. /* The TeX server may want to know that we have been called */
  52. CALL SETCLIP("TEXTIME",TIME('s'))
  53.  
  54. PARSE ARG filename number .
  55.  
  56. /*say address()*/
  57. /* if the TeX 'e' command was used to call the editor, don't ask if
  58.         files should be loaded. */
  59. IF address() = 'TeX-Rexx-Port' & "EDIT" ~= UPPER(mygetenv("TEXREXX"))
  60. THEN askload = 0
  61. ELSE askload = 1
  62.  
  63. /**
  64. OPTIONS RESULTS
  65. namestruc filename
  66. IF 0~= RC THEN DO
  67.         say "TeXEdit.rexx: Function "namestruc" not found!"
  68.         EXIT 10
  69.         END
  70. ELSE PARSE VALUE RESULT WITH ivol idirs ibase .
  71. **/
  72. PARSE VALUE namestruc(filename) WITH ivol idirs ibase .
  73.  
  74. /* The idea behind the following statements is to get an absolute path
  75. for <filename>. The result will be stored in <errnfile>. For example, "CD
  76. TeX:bar" "virtex foo" should give <filename>=foo, and
  77. <errnfile>=TeX:bar/foo. This is necessary because the editor doesn't
  78. know where file foo is located, but will be able to load file
  79. TeX:bar/foo. */
  80.  
  81. mainname = GETCLIP("TEXFILE")
  82. if "" == mainname THEN DO
  83.    /* TeX has not been started through some Start_TeX.#? script. */
  84.    /* We have a hard time finding the right directories */
  85.  
  86.    texdir = pragma('d')
  87.  
  88.    /* Amiga OS dirnames should end with either : or /
  89.         thus you need just append the filename */
  90.    IF RIGHT(texdir,1)~=':' & RIGHT(texdir,1)~='/' THEN texdir = texdir||'/'
  91.  
  92.    IF 0 = ivol THEN DO
  93.         errnfile = texdir||SUBSTR(filename, 1+ivol)
  94.         /* The logfile is in the current dir */
  95.         logfile = texdir||SUBSTR(filename, 1+ivol+idirs, ibase)||".log"
  96.         END
  97.    ELSE DO
  98.         errnfile=filename
  99.         IF 0 = idirs THEN
  100. /* This is a bug in virtex from PasTeX12a (made Jan 30 1991 [br]/[hes])
  101.         The logfile is *not* in virtex's directory, but rather in the
  102. source's directory if a device name, but no subsequent directory was
  103. given. I.e, virtex (CD in RAM) TeX:sample would put the errorfile in
  104. TeX: and not in RAM: as it should be. */
  105.            logfile = LEFT(filename, ivol+idirs+ibase)||".log"
  106.         ELSE logfile = texdir||SUBSTR(filename, 1+ivol+idirs, ibase)||".log"
  107.         END
  108.    DROP texdir
  109.    END
  110.  
  111. ELSE DO /* TeX was started through Start_TeX.#? scripts */
  112.         /* "TEXFILE" tells us CD and the main file */
  113. /**
  114.    namestruc mainname
  115.    PARSE VALUE RESULT WITH mvol mdirs mbase .
  116. **/
  117.    PARSE VALUE namestruc(mainname) WITH mvol mdirs mbase .
  118.  
  119.    IF 0 ~= ivol THEN errnfile = filename
  120.    ELSE errnfile = LEFT(mainname, mvol+mdirs)||SUBSTR(filename, 1+ivol)
  121.    logfile = LEFT(mainname, mvol+mdirs+mbase)||".log"
  122.    DROP mvol mdirs mbase
  123.    END
  124.  
  125.  
  126. /* 0 = ibase would mean that the call was incorrect, for example when
  127.    TEXREXXEDIT says "TeXedit.rexx" and not "TeXedit.rexx %s %d" */
  128. IF 0 = ibase | ~EXISTS(errnfile) THEN DO
  129.         say "TeXedit.rexx: Can't find erroneous file."
  130.         EXIT 10
  131.         END
  132. ELSE IF ~EXISTS(logfile) THEN DO
  133.         say "TeXedit.rexx: Can't find log file."
  134.         logfile = ""
  135.         /* but we continue */
  136.         END
  137.  
  138. DROP ivol idirs ibase
  139.  
  140. /********************************************************/
  141. /*      Here starts each editor specific parts          */
  142.  
  143. /************************/
  144. /*      Cygnus Ed       */
  145. IF SHOW('P', 'rexx_ced') THEN DO
  146.  
  147. ADDRESS "rexx_ced"
  148. OPTIONS RESULTS
  149.  
  150. cedtofront
  151.  
  152. /* testen ob die Files nicht schon geladen sind. (hes) */
  153.  
  154. /* the following should not be commented out, as this macro is called
  155. asynchronously, and it would be very bad for the user to have it's
  156. input mixed with the newly loaded files. Popping up a requester
  157. ensures that the user does not type in something and waits for the
  158. files to be loaded. This is the method I use for synchronization (JCH) */
  159.  
  160. IF askload THEN DO
  161.         okay2 'TeX found an error in 'errnfile'. Load files ?'
  162.         IF 1 ~= RESULT THEN EXIT 0
  163.         END
  164.  
  165. /* TODO: We should really delete old logfiles automatically, without letting
  166.         CED open a requester to ask if an old modified file may be
  167.         overwritten or not */
  168.  
  169. IF "" ~= logfile THEN DO
  170.         'jump to file "'logfile'"'
  171.         IF 0 ~= RESULT THEN Quit /* always assume the logfile currently
  172.                 loaded is old, because TeX generated a new one. */
  173.         Open new
  174.         /*expand view*/
  175.         open '"'logfile'"'
  176.         editable file   /* now it's non-editable */
  177.         beg of file
  178.         /* if we don't have the line number: search the number in the logfile (hes) */
  179.         /* bringt das wirklich etwas ? Der erste Fehler steht
  180.                 sowieso gegen Anfang der Logdatei */
  181.         IF 0 ~= number THEN 'Search for...' "l."||number
  182.         ELSE DO
  183.           'Search for...' "l." /* ^M bringt nichts, und dann Steuercodes hier...*/
  184.           'Right'
  185.           'Status 55'           /* take the current line from CED */
  186.           PARSE VAR RESULT "l."number .
  187.         END
  188.         /* changed from Search "! " (hes) */
  189.         END
  190. ELSE    okay1 "Couldn''t find logfile associated with TeX error!"
  191.  
  192. /* the following line should really read "jump to file errnfile" and
  193. not filename, because I have done extra work to get the right
  194. directory. But this information is only in errnfile, which contains an
  195. absolute pathname, and not in filename, which is the parameter
  196. supplied by virtex or initex, usually a relative pathname. CED's
  197. current directory is not necessarily virtex'es one. It also seems that
  198. CED is not smart enough as to make the difference between
  199. foo:tgmoae/myfile and bar:tgmoab/myfile. If I say jump to file
  200. foo:tgmoae/myfile it may as well jump to bar:tgmoab/myfile, whichever
  201. window comes first. */
  202.  
  203. 'jump to file "'errnfile'"'
  204. IF 0 = RESULT THEN DO
  205.         open new
  206.         open '"'errnfile'"'
  207.         END
  208.  
  209. /*expand view*/
  210.  
  211. /* TODO: another editor may have modified the disk file, or the user
  212. the currently loaded file, while virtex compiled an old version. How
  213. can I get rid of that stupid filerequester in that case? I said "open
  214. errnfile", so why does CED pop up a filerequester (and may put the
  215. user in the wrong directory too) ? */
  216.  
  217. IF 0~= number THEN jumpto number
  218. beg of line
  219.  
  220. EXIT 0
  221. END     /* End of CED part */
  222.  
  223.  
  224. /****************************************************************/
  225. /*      Micro(Gnu)Emacs (MG), to be found on AmigaLibDisk352    */
  226. IF SHOW('P','mg') THEN DO
  227.  
  228. ADDRESS 'mg'
  229. OPTIONS RESULTS
  230.  
  231. /* The following waits until MG is deiconified */
  232. 'amiga-window-to-top'   /* should activate window ? */
  233.  
  234. /* we need to prevent the user from continuing to type and thus
  235. modifying the newly created buffers. I choose the rexx-lock/unlock
  236. method to synchronise with the user because MG3b4 has some cursor
  237. position/display bugs: the cursor may erroneously appear in some
  238. buffer and overwrite it (on the screen only), and not appear in the
  239. bottom line where it belongs. */
  240.  
  241. IF askload THEN DO
  242.         'rexx-lock'     /* this is dangerous if this script aborts */
  243.         ADDRESS VALUE RESULT
  244.  
  245.         'rexx-request "TeX found an error. Load files ? "'
  246.         retc=RC
  247.         rets=RESULT
  248.  
  249.         'rexx-unlock'
  250.         ADDRESS VALUE RESULT
  251.  
  252.         IF 1 < retc /* abort */ | LEFT(UPPER(rets),1)='N' THEN DO
  253.                 'rexx-display ""'
  254.                 EXIT 0
  255.                 END
  256.         DROP retc rets
  257.         END
  258.  
  259. 'find-file "'errnfile'"'
  260. /* MG doesn't seem to set the RC value on a find-file */
  261. 'rexx-buffer' buf
  262. /* buf.1 is the buffer name, buf.3 the number of lines */
  263. IF number < buf.3 THEN DO
  264.         'delete-other-windows'
  265.         IF "" ~= logfile THEN DO
  266.                 'split-window-vertically'
  267.                 /* 'other-window' */
  268.  
  269. /* now get rid of old logfiles. Here I make sure that I get rid of
  270. every suspicious logfile, because multiple pathes may lead to the same
  271. file, as for example DH0:TeX/sample.log & TeX:sample.log */
  272.  
  273.                 'rexx-buffer-list' buffers
  274.                 PARSE VALUE namestruc(logfile) WITH ivol idirs ibase
  275.                 logname=UPPER(SUBSTR(logfile,1+ivol+idirs))
  276.                 DO i=1 TO buffers.0
  277.                    IF 0 < INDEX(UPPER(buffers.i.file),logname) THEN DO
  278.                         'switch-to-buffer "'buffers.i.name'"'
  279.                         'not-modified'
  280.                         'kill-buffer "'buffers.i.name'"'
  281.                         END
  282.                    END
  283.  
  284.                 'find-file "'logfile'"'
  285.                 /* mg doesn't seem to set the RC value */
  286.                 'beginning-of-buffer'
  287.                 IF 0 ~= number THEN 're-search-forward "^l."number'
  288.                 ELSE DO
  289.                    /* try to use normal search ? */
  290.                    're-search-forward "^l."'    /* ^ means beg. of line */
  291.                    IF 0 = RC THEN DO    /* search successfull */
  292.                         'rexx-line'     /* get current line contents */
  293.                         PARSE VAR RESULT "l."number .
  294.                         /*number = WORD(RESULT, 2) ?*/
  295.                         END
  296.                    END
  297.                 'other-window'
  298.                 'rexx-display "Now what''s that error?"'
  299.                 END
  300.         ELSE DO
  301.                 'rexx-display "Couldn''t find logfile associated with TeX error!"'
  302.                 /* EXIT 0 * 5? */
  303.                 END
  304.         IF 0 ~= number THEN 'goto-line 'number
  305.         END
  306. ELSE DO
  307.         'not-modified'
  308.         'kill-buffer "'buf.name'"'      /* kill this empty buffer */
  309.         'rexx-display "Couldn''t find erroneous file !"'
  310.         EXIT 5
  311.         END
  312. EXIT 0
  313. END     /* End of MG part*/
  314.  
  315.  
  316. /***********************************************/
  317. /*                    GoldED                   */
  318. /* Done by R.Laederach                         */
  319.  
  320. IF SHOW('P', 'GOLDED.1') THEN DO /* Here I should get the right ARexx port on */
  321.                                  /* the spot, but how can I know which GoldED */
  322.                                  /* was used to edit the text when coming from*/
  323. ADDRESS "GOLDED.1"               /* virtex? */
  324. OPTIONS RESULTS
  325.  
  326. 'SCREEN FRONT'
  327.  
  328. 'LOCK CURRENT'
  329.  
  330. IF askload THEN DO
  331.     'REQUEST BUTTON="_Yep!|_Nope!" BODY="TeX found an error in the TeX source file. Load Files?"'
  332.     if (RESULT = 'TRUE') THEN EXIT 0
  333.     END
  334.  
  335. IF "" ~= logfile THEN DO
  336.     'REQUEST HIDE = TRUE'
  337.     'WINDOW USE' logfile
  338.     IF RC < 0 THEN QUIT
  339.     'REQUEST HIDE = FALSE'
  340.     MORE
  341.     'WINDOW HEIGHT' 80 /* I still want to see my text */
  342.     'OPEN NAME' logfile
  343.     'GOTO TOP'
  344.     IF 0 ~= number THEN 'FIND STRING=l.'||number
  345.     else DO
  346.         'FIND STRING="l."'
  347.         'FIRST'
  348.         'QUERY BUFFER'
  349.         PARSE VAR RESULT "l." number errorstring
  350.         IF "" ~= errorstring THEN DO
  351.             wordnumber = WORDS(errorstring)
  352.             errorword = WORD(errorstring,wordnumber)
  353.         END
  354.     END
  355. END
  356.     else DO
  357.         'REQUEST BODY="Could not find logfile associated with TeX error"'
  358.         UNLOCK
  359.         END
  360.  
  361. 'WINDOW USE' errnfile
  362. notok = (RC ~= 0)
  363. if (notok) THEN DO
  364.     MORE
  365.     'OPEN NAME' errnfile
  366.     END
  367.  
  368. IF 0 ~= number THEN DO
  369.     'GOTO LINE' number
  370.     FIRST
  371.     'FIND QUIET NEXT STRING='||errorword /* Got him! */
  372.     UNLOCK  /* Important: Calling unlock.ged sucks! */
  373.     END
  374.  
  375. EXIT 0
  376. END          /* End of GoldED part */
  377.  
  378.  
  379. SAY "TeXedit.rexx: No supported editor active."
  380. EXIT 10
  381.  
  382. mygetenv: procedure     /* when will ARexx supply GetEnv/SetEnv ? */
  383.    PARSE ARG name
  384.  
  385.    IF open(TEMPFILE,"ENV:"||name,'r') THEN DO
  386.         gives = readln(TEMPFILE)
  387.         CALL close TEMPFILE
  388.         END
  389.    ELSE gives = ""
  390.  
  391.    RETURN gives
  392.